export default {
  created() {
    this.getData();
  },
  data() {
    return {
      // 商品列表数据
      reqParams: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      goodsList: [],
      total: 0
    };
  },
  methods: {
    // 分页功能
    pager(newPagenum) {
      this.reqParams.pagenum = newPagenum;
      this.getData();
    },
    async getData() {
      const {
        data: { data, meta }
      } = await this.$http.get("goods", { params: this.reqParams });
      if (meta.status !== 200)
        return this.$message.error("获取商品列表数据失败");
      this.goodsList = data.goods;
      this.total = Math.ceil(data.total / this.reqParams.pagesize);
    },
    search() {
      this.getData();
    },
    delGoods(id){
      this.$confirm("确定删除该商品吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          const {
            data: { meta }
          } = await this.$http.delete(`goods/${id}`);
          // 失败情况
          if (meta.status !== 200) return this.$message.error("删除商品失败");
          // 成功情况
          this.getData();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    addGoods() {
      this.$router.push('/addgoods');
    }
  }
};
